package cn.shop.merchant.service;

import cn.shop.merchant.req.BlacklistQueryReq;
import cn.shop.transcation.blacklist.entity.Blacklist;
import cn.shop.transcation.blacklist.mapper.BlacklistMapper;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-8 17:10
 * @description : 黑名单service
 */
@Service
public class BlacklistService extends BaseServiceImpl<BlacklistMapper, Blacklist> {

    /**
     * 黑名单列表
     *
     * @param req 查询条件
     * @param uid 商户编号
     */
    public Map<String, Object> blacklists(long uid, BlacklistQueryReq req) {
        Page<Blacklist> page = Page.of(req.getCurrent(), req.getSize());
        LambdaQueryWrapper<Blacklist> query = Wrappers.<Blacklist>lambdaQuery()
                .like(notNull.test(req.getAlipay()), Blacklist::getAlipayAccount, req.getAlipay())
                .eq(Blacklist::getUid, uid)
                .apply(notNull.test(req.getStartTime()), req.gtTime("create_time"), req.getStartTime())
                .apply(notNull.test(req.getEndTime()), req.ltTime("create_time"), req.getEndTime())
                .orderBy(notZero.test(req.getOrderByTime()), isAsc.test(req.getOrderByTime()), Blacklist::getId);
        page = page(page, query);
        return PageUtil.formatPage(page, this::format);
    }

    /**
     * 更新黑名单
     *
     * @param blacklist 黑名单
     * @param uid 商户编号
     */
    public boolean updateBlacklist(Blacklist blacklist, long uid) {
        return update(blacklist, Wrappers.<Blacklist>lambdaUpdate()
                .eq(Blacklist::getUid, uid)
                .eq(Blacklist::getId, blacklist.getId()));
    }

    /**
     * 删除黑名单
     *
     * @param ids 黑名单编号
     * @param uid 商户编号
     */
    public boolean deleteBlacklist(long[] ids, long uid) {
        int count = getBaseMapper().batchDeleteWithFill(new Blacklist(), Wrappers.<Blacklist>lambdaQuery()
                .in(Blacklist::getId, Arrays.stream(ids).boxed().collect(Collectors.toList()))
                .eq(Blacklist::getUid, uid));
        return count > 0;
    }

    /**
     * 获取黑名单信息
     *
     * @param id 黑名单编号
     * @param uid 商户编号
     */
    public Map<String, Object> info(long id, long uid) {
        Blacklist blacklist = getOne(Wrappers.<Blacklist>lambdaUpdate()
                .eq(Blacklist::getId, id)
                .eq(Blacklist::getUid, uid));
        return format(blacklist);
    }

    private Map<String, Object> format(Blacklist blacklist) {
        return Putif.start()
                .anyway("id", blacklist.getId())
                .anyway("uid", blacklist.getUid())
                .anyway("alipayAccount", blacklist.getAlipayAccount())
                .anyway("remark", blacklist.getRemark())
                .anyway("createTime", blacklist.getCreateTime())
                .get();
    }
}
